/*
 * Copyright (C) 2010,2011.
 * AHCP Project
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at 
 *
 *     http://www.apache.org/licenses/LICENSE-2.0 
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 
 * express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package org.jacp.demo.main;

import java.awt.Container;
import java.awt.LayoutManager2;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import org.im4java.process.ProcessStarter;
import org.jacp.api.workbench.IWorkbench;
import org.jacp.impl.Launcher;
import org.jacp.impl.SpringLauncher;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Application Launcher (main class); JACP ImageApplication Demo
 * 
 * @author Andy Moncsek
 * 
 */
public class ImageDemoLauncher {

    private static String homePath;

    static {
	try {
	    try {
		homePath = new File(".").getCanonicalPath();
	    } catch (final IOException e) {
		e.printStackTrace();
	    }
	    setOsSpecificSettings();
	} catch (final ClassNotFoundException ex) {
	    Logger.getLogger(ImageDemoLauncher.class.getName()).log(
		    Level.SEVERE, null, ex);
	}
    }

    @SuppressWarnings("unchecked")
    public static void main(final String[] args) {
	final Launcher<ClassPathXmlApplicationContext> launcher = new SpringLauncher(
		"org/jacp/demo/resource/main.xml");
	final IWorkbench<Container, LayoutManager2, ActionListener, ActionEvent, Object> workbench = (IWorkbench<Container, LayoutManager2, ActionListener, ActionEvent, Object>) launcher
		.getContext().getBean("workbench");
	workbench.init(launcher);
    }

    private static void initImageMagic(final String osType) {
	ProcessStarter.setGlobalSearchPath("/opt/local/bin/");
    }

    private static void setOsSpecificSettings() throws ClassNotFoundException {
	final String osName = System.getProperty("os.name");
	if (osName.toLowerCase().trim().contains("mac")) {
	    setOSXspecific();
	    initImageMagic("darwin_x86_64");
	} else {
	    setDefault();
	}
    }

    private static void setOSXspecific() throws ClassNotFoundException {
	System.setProperty("com.apple.mrj.application.apple.menu.about.name",
		"JACP Demo");
	System.setProperty("apple.laf.useScreenMenuBar", "true");
	try {
	    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
	} catch (final InstantiationException ex) {
	    Logger.getLogger(ImageDemoLauncher.class.getName()).log(
		    Level.SEVERE, null, ex);
	} catch (final IllegalAccessException ex) {
	    Logger.getLogger(ImageDemoLauncher.class.getName()).log(
		    Level.SEVERE, null, ex);
	} catch (final UnsupportedLookAndFeelException ex) {
	    Logger.getLogger(ImageDemoLauncher.class.getName()).log(
		    Level.SEVERE, null, ex);
	}

    }

    private static void setDefault() throws ClassNotFoundException {
	try {
	    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
	} catch (final InstantiationException ex) {
	    Logger.getLogger(ImageDemoLauncher.class.getName()).log(
		    Level.SEVERE, null, ex);
	} catch (final IllegalAccessException ex) {
	    Logger.getLogger(ImageDemoLauncher.class.getName()).log(
		    Level.SEVERE, null, ex);
	} catch (final UnsupportedLookAndFeelException ex) {
	    Logger.getLogger(ImageDemoLauncher.class.getName()).log(
		    Level.SEVERE, null, ex);
	}

    }

    public static File getCacheDir() {
	return new File(homePath + "/cache/");
    }

}
